<?php
/*
* $Author ：PHPYUN开发团队
*
* 官网: http://www.phpyun.com
*
* 版权所有 2009-2018 宿迁鑫潮信息技术有限公司，并保留所有权利。
*
* 软件声明：未经授权前提下，不得用于商业运营、二次开发以及任何形式的再次发布。
 */
class company_controller extends wxapp_controller{
	function list_action(){//职位列表
		$where="`name`<>'' and `r_status`<>'2'";
	    if ($_POST['keyword']!='undefined'){
		    $keyword=$this->stringfilter($_POST['keyword']);
		}
		$page=$_POST['page'];
		$limit=$_POST['limit'];
		$order=$_POST['order'];
		$nodata=$_POST['nodata'];
		$uid=(int)$_POST['uid'];
		$provinceid=(int)$_POST['provinceid'];
		$cityid=(int)$_POST['cityid'];
		$three_cityid=(int)$_POST['three_cityid'];
		$hy=(int)$_POST['hy'];
		$pr=(int)$_POST['pr'];
		$mun=(int)$_POST['mun'];
		$rec = (int)$_POST['rec'];
		$limit=!$limit?10:$limit;
		if($hy){//类别ID
			$where.=" and `hy`='".$hy."'";
		}
		if($provinceid){//类别ID
			$where.=" and `provinceid`='".$provinceid."'";
		}
		if($cityid){//类别ID
			$where.=" and `cityid`='".$cityid."'";
		}
		if($three_cityid){//类别ID
		    $where.=" and `three_cityid`='".$three_cityid."'";
		}
		if($keyword){//关键字
			$where.=" and `name` like '%".$keyword."%'";
		}
		if($uid){//UID
			$where.=" and `uid`='".$uid."'";
		}
		if($rec==1){//名企
			$where.=" and `rec`='1' AND `hottime`>'".time()."'";
		}
		if($pr){//企业性质
			$where.=" and `pr`='".$pr."'";
		}
		if($mun){//企业规模
			$where.=" and `mun`='".$mun."'";
		}
		if($_POST['firmpic']){//名企图片
			$where.=" and `firmpic`<>''";
		}
		if($nodata){//排除没有值的字段
			$nodataarr=explode(",",$nodata);
			foreach($nodataarr as $v){
				$where.=" and ".$v."<>''";
			}
		}
		if($order){//排序
			$where.=" order by ".$order;
		}else{
			$where.=" order by lastupdate desc";
		}
		if($page){//分页
			$pagenav=($page-1)*$limit;
			$where.=" limit $pagenav,$limit";
		}else{
			$where.=" limit $limit";
		}
		
		$rows=$this->obj->DB_select_all("company",$where);
		if($rows && is_array($rows)){
		    include PLUS_PATH."/city.cache.php";
		    include PLUS_PATH."/industry.cache.php";
		    $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));//今天开始时间戳
		    $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));//昨天开始时间戳
		    $week=strtotime(date("Y-m-d",strtotime("-1 week")));//一周内时间戳
			foreach($rows as $key=>$k){
			    $uids[]=$k['uid'];
				$list[$key]['uid']			=$k['uid'];
				if($k['shortname']){
					$list[$key]['name']			=mb_substr($k['shortname'], 0,16,'utf-8');
				}else{
					$list[$key]['name']			=mb_substr($k['name'], 0,16,'utf-8');
				}
				
				$list[$key]['hy']			=$k['hy'];
				$list[$key]['pr']			=$k['pr'];
				$list[$key]['provinceid']	=$k['provinceid'];
				$list[$key]['cityid']		=$k['cityid'];
				$list[$key]['mun']			=$k['mun'];
				$list[$key]['sdate']		=$k['sdate'];
				$list[$key]['money']		=$k['money'];
				$list[$key]['content']		=$k['content'];
				$list[$key]['address']		=$k['address'];
				$list[$key]['zip']			=$k['zip'];
				$list[$key]['linkman']		=$k['linkman'];
				$list[$key]['linkjob']		=$k['linkjob'];
				$list[$key]['linkqq']		=$k['linkqq'];
				$list[$key]['linkphone']	=$k['linkphone'];
				$list[$key]['linktel']		=$k['linktel'];
				$list[$key]['linkmail']		=$k['linkmail'];
				$list[$key]['x']			=$k['x'];
				$list[$key]['y']			=$k['y'];
				$list[$key]['lastupdate']	=$k['lastupdate'];
				$list[$key]['jobtime']		=$k['jobtime'];
				$list[$key]['r_status']		=$k['r_status'];
				$list[$key]['firmpic']		=$this->config['sy_weburl'].str_replace('./','/',$k['firmpic']);
				$list[$key]['rec']			=$k['rec'];
				$list[$key]['hits']			=$k['hits'];
				$list[$key]['ant_num']		=$k['ant_num'];
				$list[$key]['pl_time']		=$k['pl_time'];
				$list[$key]['pl_status']	=$k['pl_status'];
				$list[$key]['order']		=$k['order'];
				$list[$key]['admin_remark']	=$k['admin_remark'];
				$list[$key]['email_dy']		=$k['email_dy'];
				$list[$key]['msg_dy']		=$k['msg_dy'];
				//小程序所需汉字字段-start
				$list[$key]['hy_n']         =$industry_name[$k['hy']];
				$list[$key]['city']         =$city_name[$k['cityid']]?$city_name[$k['provinceid']].'-'.$city_name[$k['cityid']]:$city_name[$k['provinceid']];
				if ($k['lastupdate']>$week && $k['lastupdate']<$beginYesterday){
				    $list[$key]['lastupdate_n']="一周内";
				}elseif($k['lastupdate']>$beginYesterday && $k['lastupdate']<$beginToday){
				    $list[$key]['lastupdate_n']="昨天";
				}elseif($k['lastupdate']>$beginToday){
				    $list[$key]['lastupdate_n'] = date("H:i",$k['lastupdate']);
				}else{
				    $list[$key]['lastupdate_n'] = date("Y-m-d",$k['lastupdate']);
				}
				if($k['logo'] && (file_exists(str_replace('./',APP_PATH,$k['logo'])))){
				    $list[$key]['logo_n']=str_replace("./",$this->config['sy_weburl']."/",$k['logo']);
				}else{
				    $list[$key]['logo_n']=$this->config['sy_weburl'].'/'.$this->config['sy_unit_icon'];
				}
				if($k['hottime']>time()){
				    $list[$key]['rec_n']=1;
				}
				//小程序所需汉字字段-end
			}
			if($uids){
			    $job_rows=$this->obj->DB_select_all("company_job","`uid` in (".pylode(',', $uids).") and `r_status`<>'2' and `status`<>'1' and `state`=1","`id`,`uid`,`name`");
			}
			foreach($list as $k=>$v){
			    $list[$k]['jobnum']=0;
			    foreach ($job_rows as $key=>$val){
			        if($v['uid']==$val['uid']){
			            $list[$k]['joblist'][$key]['name']=mb_substr($val['name'], 0,10,'utf-8');
			            $list[$k]['joblist'][$key]['id']=$val['id'];
			            $list[$k]['jobnum']=$list[$k]['jobnum']+1;
			        }
			    }
				if(is_array($v)){
					foreach($v as $key=>$val){
						$list[$k][$key]=isset($val)?$val:'';
					}
				}else{
					$list[$k]=isset($v)?$v:'';
				}
			}
			$data['list']=count($list)?$list:array();
			$data['error']=1;
		}else{
			$data['error']=2;
		}
		echo json_encode($data);die;
	}
	function show_action(){//获取企业信息
		$id=(int)$_POST['id'];
		$uid=(int)$_POST['uid'];
		$usertype=(int)$_POST['usertype'];
		$row = $this->obj->DB_select_once("company","`uid`='".$id."'");
		if((!is_array($row) || empty($row)) && $id){
			$data['error']=2;
			echo json_encode($data);die;
		}
		$com = $this->obj->DB_select_once("member","`uid`='".$id."'");
		if((!is_array($com) || empty($com)) && $id){
			$data['error']=2;
			echo json_encode($data);die;
		}
		$data=array();
		include PLUS_PATH."/city.cache.php";
		include PLUS_PATH."/com.cache.php";
		include PLUS_PATH."/industry.cache.php";

		$data['uid']			=$row['uid'];
		if($row['shortname']){
			$data['name']		=mb_substr($row['shortname'], 0,16,'utf-8');
		}else{
			$data['name']		=mb_substr($row['name'], 0,16,'utf-8');
		}
		//用户是否关注该商家
		$atninfo = $this->obj->DB_select_once("atn","`uid`='".$uid."' AND `sc_uid`='".$id."' AND (`xjhid`=0 || `xjhid` is NULL)");
		if($atninfo&&is_array($atninfo)){
		    $data['isatn'] = 1;
		}else{
		    $data['isatn'] = 0;
		}
		
		$data['lastupdate_n']=date("Y-m-d",$row['lastupdate']);
		$data['city']        =$city_name[$row['cityid']]?$city_name[$row['provinceid']].$city_name[$row['cityid']]:$city_name[$row['provinceid']];
		if($row['logo'] && (file_exists(str_replace('./',APP_PATH,$row['logo'])))){
		    $data['logo_n']=str_replace("./",$this->config['sy_weburl']."/",$row['logo']);
		}else{
		    $data['logo_n']=$this->config['sy_weburl'].'/'.$this->config['sy_unit_icon'];
		}
		$data['pr_n']        =$comclass_name[$row['pr']];
		$data['mun_n']       =$comclass_name[$row['mun']];
		$data['hy_n']        =$industry_name[$row['hy']];
	    if ($row['moneytype']==2){
		    $data['moneytype_n']="万美元";
		}else{
		    $data['moneytype_n']="万人民币";
		}
		$content = preg_replace("/<([a-zA-Z] )[^>]*>/","<$1>",$row['content']);
		$content=$this->preghtml($content);
		$content = str_replace('<img','<p style="text-align:center;"><img',$content);
		$content=htmlspecialchars_decode($content);
		preg_match_all('<img(.*?)src=\"(.+?)\".*?>',$content,$res);
		if(!empty($res[2])){
		    foreach($res[2] as $v){
		        if(strpos($v,'https:')===false && strpos($v,'http:')===false){
		            $imgurl=$v;
		            $content=str_replace($v,$this->config['sy_weburl'].$imgurl,$content);
		        }
		    }
		}
		$content=str_replace("<>","",$content);
		$data['content']     =str_replace(array("/\r\n\r\n\r\n/","&nbsp;"), array("\r\n",""),$content);
		$data['money']		=$row['money'];
		if($this->config['com_login_link']=='2'){
		    $data['looktype']=2;
		    $data['lookmsg']="网站没有开放企业联系信息！";
		}elseif($this->config['com_login_link']=='1'){
		    if($row['infostatus']==2){
		        $data['looktype']=3;
		        $data['lookmsg']="企业没有公开联系信息！";
		    }else{
		        $data['looktype']=1;
		    }
		}elseif($this->config['com_login_link']=="3"){
		    if($uid==''){
				$data['linkman']=$row['linkman'];
				$data['address']=$row['address'];
		        if($row['linktel']){
		            $data['linktel']= substr_replace($row['linktel'],'****',4,4);
		        }
		        if($row['linkphone']){
		            $data['linkphone']= substr_replace($row['linkphone'],'****',4,4);
		        }
		        $data['looktype']=4;
		    }elseif($usertype!=1&&$id!=$uid){
		        $data['lookmsg']="您不是个人用户，不能查看联系方式";
		        $data['looktype']=5;
		    }elseif($usertype==2&&$id==$uid){
		        $data['looktype']=1;
		    }else{
		        if($this->config['com_resume_link']=="1"){
		            $Resume=$this->MODEL('resume');
		            $resumenum=$Resume->GetResumeExpectNum(array("uid"=>$uid));
		            if($row['infostatus']==2){
		                $data['lookmsg']="企业没有公开联系信息！";
		                $data['looktype']=3;
		            }else{
		                if($resumenum<1){
		                    $data['lookmsg']="您还没有创建简历，不能查看联系方式！";
		                    $data['looktype']=6;
		                }else{
		                    $data['looktype']=1;
		                }
		            }
		        }else{
		            if($row['infostatus']==2){
		                $data['lookmsg']="企业没有公开联系信息！";
		                $data['looktype']=3;
		            }else{
		                $data['looktype']=1;
		            }
		        }
		    }
		}
		if($data['looktype']==1){
			$data['linkman']=$row['linkman'];
			$data['linktel']=$row['linktel'];
			$data['linkphone']=$row['linkphone'];
			$data['busstops']=$row['busstops'];
			$data['address']=$row['address'];
		}
		//海报判断
		$data['hbshow']=2;
// 		if($this->config["sy_haibaouser"] && $this->config["sy_haibaopw"] && $this->config["sy_haibaokey"] && $this->config['sy_haibao_isopen']=='1'){
// 		    $data['hbshow']=1;
// 		}
		foreach($data as $k=>$v){
			if(is_array($v)){
				foreach($v as $key=>$val){
					$data[$k][$key]=isset($val)?$val:'';
				}
			}else{
				$data[$k]=isset($v)?$v:'';
			}
		}
		$data['error']=1;
		echo json_encode($data);die;
	}
	function ws_action(){
	    if(!$this->config["sy_haibaouser"] || !$this->config["sy_haibaopw"] || !$this->config["sy_haibaokey"]||$this->config['sy_haibao_isopen']!='1'){
	        $this->errjson('还没有配置海报，请联系管理员',10);
	    }else {
	        $haibaouser=iconv('utf-8','gbk',trim($this->config['sy_haibaouser']));
	        $key=trim($this->config['sy_haibaokey']);
	        $url='http://haibao.phpyun.com/notice.php';
	        $url.="?haibaouser=".$haibaouser."&key=".$key;
	        
	        if (extension_loaded('curl')){
	            $return = CurlGet($url);
	        }else if(function_exists('file_get_contents')){
	            $return = file_get_contents($url);
	        }
	        
	        if($return <= 0){
	            $this->errjson('海报生成已达上限，请明日再来',11);
	        }else{
	            $this->errjson('ok',0);
	        }
	    }
	}
}
?>